
var frameDuration = 1000 / 60; //60 frames by seconds

var dynamicNewsAnimation = {

		NB_FRAMES_IN_CYCLE: -1,
		SCENE_TRANSITION_STEP_SIZE : -1, //nb pixels to move of.
		SCENE_TRANSITION_DURATION : -1,
		SCENE_RETURN_STEP_SIZE: -1,
		SCENE_DURATION: -1,
		WIDTH_OF_A_NEWS_BLOCK: -1,
		SCENES_IDS : ["presentation___news","actualites___news","debat_d_idees___news","lectures_conseillees___news"],
		START_POS_X_FOR_DISPLAY_SCENE : -1,
		currentFrame : 0,
		currentScene : 1,
		nearestSceneOnRightId : null,
		shouldMove : false,

		init : function(sceneDuration,sceneTransitionDuration) {

			dynamicNewsAnimation.SCENE_DURATION = sceneDuration;
			dynamicNewsAnimation.SCENE_TRANSITION_DURATION = sceneTransitionDuration;
			dynamicNewsAnimation.recomputeCycle();
			dynamicNewsAnimation.NB_FRAMES_IN_CYCLE = sceneDuration * dynamicNewsAnimation.SCENES_IDS.length;
		},
		
		computeStepsSize : function() {
			var newsBlockContainerWidth = parseInt($("#news_block").css("width"))*dynamicNewsAnimation.SCENES_IDS.length;

			dynamicNewsAnimation.WIDTH_OF_A_NEWS_BLOCK = parseInt($(".news").css("width"));
			dynamicNewsAnimation.SCENE_TRANSITION_STEP_SIZE = dynamicNewsAnimation.WIDTH_OF_A_NEWS_BLOCK / dynamicNewsAnimation.SCENE_TRANSITION_DURATION;
			dynamicNewsAnimation.SCENE_RETURN_STEP_SIZE = newsBlockContainerWidth / dynamicNewsAnimation.SCENE_TRANSITION_DURATION;
			dynamicNewsAnimation.START_POS_X_FOR_DISPLAY_SCENE = $("#"+dynamicNewsAnimation.SCENES_IDS[0]).position().left;
		},

		recomputeCycle : function() {
			$("#news_block .scrolled_x_block").scrollLeft(0);
			
			dynamicNewsAnimation.computeStepsSize();
			
			dynamicNewsAnimation.currentFrame = 0;
			dynamicNewsAnimation.currentScene = 1;
			dynamicNewsAnimation.nearestSceneOnRightId = null;
			dynamicNewsAnimation.shouldMove = false;
		},
		
		animateCurrentFrame : function() {

			var isStartPos = (dynamicNewsAnimation.currentFrame == 0);
			var shouldMoveToNextScene = ((dynamicNewsAnimation.currentFrame % dynamicNewsAnimation.SCENE_DURATION) == 0);
			var isLastScene = (dynamicNewsAnimation.currentScene == dynamicNewsAnimation.SCENES_IDS.length);

			if((isStartPos == false) && (shouldMoveToNextScene == true) && (dynamicNewsAnimation.shouldMove == false)) {
				dynamicNewsAnimation.updateNearestSceneOnRightId();
				dynamicNewsAnimation.shouldMove = true;
			}

			if(dynamicNewsAnimation.shouldMove) {
				dynamicNewsAnimation.chooseAndDoTransition(isLastScene);
			}
			
			if(dynamicNewsAnimation.currentFrame <= dynamicNewsAnimation.NB_FRAMES_IN_CYCLE) {
				dynamicNewsAnimation.currentFrame++;
			}
			else {
				dynamicNewsAnimation.currentFrame = 0;
			}
		},

		updateNearestSceneOnRightId : function() {
			var nearestSceneId = null;
			var scrolledNewsBox = $("#news_block .scrolled_x_block");
			var scrollLeft = scrolledNewsBox.scrollLeft();
			var minDistance = -1;
			var isInit = false;

			dynamicNewsAnimation.nearestSceneOnRightPosX = -1;

			$(".news").each(function(index) {

				var id = $(this).attr("id");

				if(id != dynamicNewsAnimation.SCENES_IDS[0]){

					var posX = $(this).position().left;
					var distance = (posX - (dynamicNewsAnimation.START_POS_X_FOR_DISPLAY_SCENE));

					if((isInit == false) && (distance > 0)) {
						minDistance = distance;
						nearestSceneId = id;
						isInit = true;
					}
					else if ((isInit == true) && (distance >= 0) && (distance < minDistance)) {
						nearestSceneId = id;
						minDistance = distance;
					}
				}
			});	

			for(var i = 0; i < dynamicNewsAnimation.SCENES_IDS.length; i++) {
				if(nearestSceneId == dynamicNewsAnimation.SCENES_IDS[i]) {
					dynamicNewsAnimation.nearestSceneOnRightId = dynamicNewsAnimation.SCENES_IDS[i];
					break;
				}
			}
		},

		chooseAndDoTransition : function(isLastScene) {
			if(isLastScene) {
				dynamicNewsAnimation.transitionToStartScene();
			}
			else {
				dynamicNewsAnimation.transitToNextScene();
			}
		},
		
		transitToNextScene : function() {

			var scrolledNewsBox = $("#news_block .scrolled_x_block");
			var scrollLeft = scrolledNewsBox.scrollLeft();
			var nearestSceneOnRightPosX = parseInt($("#"+dynamicNewsAnimation.nearestSceneOnRightId).position().left);
			var nearestSceneOnRightPosXAfterMove = nearestSceneOnRightPosX - dynamicNewsAnimation.SCENE_TRANSITION_STEP_SIZE;

			if(dynamicNewsAnimation.START_POS_X_FOR_DISPLAY_SCENE < nearestSceneOnRightPosXAfterMove) {
				scrolledNewsBox.scrollLeft(scrollLeft+dynamicNewsAnimation.SCENE_TRANSITION_STEP_SIZE);
			}
			else {
				var diff = (dynamicNewsAnimation.SCENE_TRANSITION_STEP_SIZE - dynamicNewsAnimation.nearestSceneOnRightPosX);

				scrolledNewsBox.scrollLeft(scrollLeft+diff);
				dynamicNewsAnimation.currentScene++;
				dynamicNewsAnimation.shouldMove = false;
			}
		},

		transitionToStartScene : function() {
			var scrolledNewsBox = $("#news_block .scrolled_x_block");
			var scrollLeft = scrolledNewsBox.scrollLeft();

			if(scrollLeft > 0) {
				scrolledNewsBox.scrollLeft(scrollLeft-dynamicNewsAnimation.SCENE_RETURN_STEP_SIZE);
			}
			else {
				dynamicNewsAnimation.shouldMove = false;
				dynamicNewsAnimation.currentScene = 1;
			}
		}
};

window.addEventListener("DOMContentLoaded", function() {
	dynamicNewsAnimation.init(600,120);
});

window.addEventListener("resize", dynamicNewsAnimation.recomputeCycle);

window.setInterval(dynamicNewsAnimation.animateCurrentFrame, frameDuration);